package com.atguigu.srb.core.controller.admin;

import com.atguigu.common.exception.Assert;
import com.atguigu.common.exception.BusinessException;
import com.atguigu.common.results.ResultCodeEnum;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import com.atguigu.common.results.R ;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2022-10-24
 */
@RestController
//描述Controller的
@Api(tags = "积分等级管理模块")
@RequestMapping("/admin/core/integralGrade")
@Slf4j

public class AdminIntegralGradeController {
    @Resource
    IntegralGradeService integralGradeService;
    @ApiOperation(value = "查询所有积分等级" ,httpMethod = "GET")
    @GetMapping  //  get+/admin/core/integralGrade  查询所有
    public R list(){
        return R.success().data("items" , integralGradeService.list());
    }

    @ApiOperation("根据id删除积分等级")
    @DeleteMapping("{id}")  //  delete+/admin/core/integralGrade/{id}  根据id删除
    public R deleteById(@ApiParam(value = "积分等级id" , name = "id" ,
                                        defaultValue = "1", required = true)
                                  @PathVariable("id")String id){
        boolean b = integralGradeService.removeById(id);
        if(b){
            return R.success().message("删除成功");
        }else{
            return R.error().message("记录不存在");
        }
    }
    @ApiOperation("新增积分等级")
    @PostMapping  //  post+/admin/core/integralGrade  新增
    public R save(@RequestBody IntegralGrade integralGrade){
        if(integralGrade.getBorrowAmount()==null ||
            integralGrade.getBorrowAmount().intValue()==0 ){
            //借款额度不能为0
            throw new BusinessException(ResultCodeEnum.BORROW_AMOUNT_NULL_ERROR);
        }

        Assert.assertBigDecimalNotNull(integralGrade.getBorrowAmount());
        boolean b = integralGradeService.save(integralGrade);
        if(b){
            return R.success().message("新增成功");
        }else{
            return R.error().message("新增失败");
        }
    }


    //1、根据id查询
    @ApiOperation("根据id查询积分等级")
    @GetMapping("{id}")
    public R queryById(@PathVariable("id")String id){
        IntegralGrade integralGrade = integralGradeService.getById(id);
        return R.success().data("item",integralGrade);
    }
    //2、更新
    @ApiOperation("更新积分等级")
    @PutMapping
    public R updateById(@RequestBody IntegralGrade integralGrade){//包含id的积分等级对象
        boolean b = integralGradeService.updateById(integralGrade);
        if(b){
            return R.success().message("更新成功");
        }else{
            return R.error().message("更新失败");
        }
    }
    //3、分页查询
    @ApiOperation("分页查询积分等级")
    @GetMapping("list/{pageNum}/{pageSize}")
    public R listPage(@PathVariable("pageNum") Integer pageNum ,
                      @PathVariable("pageSize") Integer pageSize){
        Page<IntegralGrade> page = new Page<>(pageNum,pageSize);
        page = integralGradeService.page(page);
        return R.success().data("page" , page);//前端展示分页数据时 还需要记录条数 页码等信息
    }
    //4、批量删除
    // 请求参数：url?id=1&id=2&id=3  springmvc可以使用url?id=1,2,3的方式来提交id的多个值
    //           @RequestParam List<String> id

    // 请求体：[1,2,3,4]   @RequestBody List<String> id
    @DeleteMapping("batchDel")
    public R batchDel(@RequestParam List<String> ids){
        log.info("删除id列表："+ids.toString());
        integralGradeService.removeByIds(ids);
        return R.success().message("批量删除成功");
    }
}

